package com.example.demo.control;

import com.example.demo.model.Result;
import com.example.demo.model.dish;
import com.example.demo.model.user;
import com.example.demo.service.dishService;
import com.example.demo.service.userService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;

@Controller
public class dishController {
    @Autowired
    private static final Logger LOG = LoggerFactory.getLogger(dishController.class);
    @Autowired
    private dishService dishService;
    @Autowired
    private userService userService;
    @PostMapping("/api/dish/add")
    public String add(Model model, @RequestParam("dishName") String dishName,
    @RequestParam("detail") String detail,@RequestParam("count") int count,@RequestParam("num") int num,@RequestParam("price") double price){
        dish dish = new dish();
        dish = dishService.findByDishName(dishName);
        if (dish != null){
            return null;
        }
        dish.setDname(dishName);
        dish.setDetails(detail);
        dish.setNum(num);
        dish.setCount(count);
        dish.setPrice(price);
        int[] numbers = {13,14,15};
        String randomImageName = generateRandomImageName(numbers);

        dish.setImages("./images/"+randomImageName);
        int add = dishService.add(dish);
        if (add!=0){
            model.addAttribute("datalist1",dishService.getAllDishes());
            return "redirect:/index";
        }

        return null;
    }
    @PostMapping("/api/dish/delete")
    public String delete(@RequestParam("dishName") String dishName, Model model){
        dish dish = dishService.findByDishName(dishName);
        Result result = new Result();
        if (dish!=null){
            dishService.delete(dish.getDid());
            model.addAttribute("datalist1",dishService.getAllDishes());
            result.setSuccess(true);

            return "redirect:/index";
        }
        model.addAttribute("datalist1",dishService.getAllDishes());
        result.setSuccess(false);
        return "redirect:/index";
    }



    @PostMapping("/api/dish/buy")
    public String buy(Model model, @RequestParam("dishName") String dishName, HttpServletRequest request){
        dish dish = dishService.findByDishName(dishName);
        if (dish.getNum() == 0){
            LOG.info("已经卖完了");
            return null;
        }
        if (dish!=null){
            int count = dish.getCount();
            int num = dish.getNum();
            num-=1;
            count+=1;
            dish.setCount(count);
            dish.setNum(num);
            dishService.update(dish);

            Object userId = request.getSession().getAttribute("userId");
            user user = userService.findByUserId((Integer) userId);
            double userMoney = user.getMoney();
            double price = dish.getPrice();
            if (userMoney<price){
                LOG.info("钱不够");
                return null;
            }
            user.setMoney(userMoney-price);
            userService.update(user);

            model.addAttribute("datalist1",dishService.getAllDishes());
            return "redirect:/index";
        }
        return null;
    }
    @PostMapping("/api/addDish")
    public String addDish(@RequestParam("dname") String dname,@RequestParam("detail") String detail,@RequestParam("price") double price,
            @RequestParam("num") int num,Model model){
        if (!StringUtils.hasText(dname)){
            LOG.error("菜品名称为空");
            return null;
        }
        if (!StringUtils.hasText(detail)){
            LOG.error("菜品信息为空");
            return null;
        }
        dish dish = new dish();
        dish.setDname(dname);
        dish.setDetails(detail);
        dish.setPrice(price);
        dish.setNum(num);
        dish.setCount(0);
        int[] numbers = {13,14,15};
        String randomImageName = generateRandomImageName(numbers);

        dish.setImages("./images/"+randomImageName);
        dishService.add(dish);
        model.addAttribute(dishService.getAllDishes());
        return "redirect:/index";
    }
    public static String generateRandomImageName(int[] numbers) {
        // 创建一个随机数生成器
        Random random = new Random();

        // 从数组中随机选择一个数字
        int randomIndex = random.nextInt(numbers.length);
        int selectedNumber = numbers[randomIndex];

        // 组合成字符串
        String imageName = selectedNumber + ".jpg";

        return imageName;
    }
}
